#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio/consumer.h>

#define HELLO_DBG
#ifdef HELLO_DBG
#define hello_dbg(fmt, ...) printk("[%s][%d]" fmt "\r\n", __func__, __LINE__, ##__VA_ARGS__)
#else
#define hello_dbg(fmt, ...)
#endif

#define MODULE_NAME "hello_spi_tftlcd"

struct hello_spi_tftlcd_desc
{
    struct gpio_desc *blk;
    struct gpio_desc *buttona;
    struct gpio_desc *buttonb;
};

static struct hello_spi_tftlcd_desc hello_desc = {};

static int hello_spi_tftlcd_probe(struct platform_device *pdev)
{
    int buttona_value;
    int buttonb_value;

    hello_dbg("");

    hello_desc.blk = devm_gpiod_get(&pdev->dev, "backlight", GPIOD_OUT_HIGH);
    if (IS_ERR(hello_desc.blk))
    {
        dev_err(&pdev->dev, "failed to get backlight gpio from dts\n");
        return PTR_ERR(hello_desc.blk);
    }

    hello_desc.buttona = devm_gpiod_get(&pdev->dev, "buttona", GPIOD_IN);
    if (IS_ERR(hello_desc.buttona))
    {
        dev_err(&pdev->dev, "failed to get buttona gpio from dts\n");
        return PTR_ERR(hello_desc.buttona);
    }
    buttona_value = gpiod_get_value(hello_desc.buttona);
    hello_dbg("buttona_value:%d", buttona_value);

    hello_desc.buttonb = devm_gpiod_get(&pdev->dev, "buttonb", GPIOD_IN);
    if (IS_ERR(hello_desc.buttonb))
    {
        dev_err(&pdev->dev, "failed to get buttonb gpio from dts\n");
        return PTR_ERR(hello_desc.buttonb);
    }
    buttonb_value = gpiod_get_value(hello_desc.buttonb);
    hello_dbg("buttonb_value:%d", buttonb_value);

    return 0;
}

static int hello_spi_tftlcd_remove(struct platform_device *)
{
    hello_dbg("");

    return 0;
}

static const struct of_device_id hello_spi_tftlcd_table[] = {
    {.compatible = "hello,spi_tftlcd"},
    {},
};

static struct platform_driver dri = {
    .probe = hello_spi_tftlcd_probe,
    .remove = hello_spi_tftlcd_remove,
    .driver = {
        .name = MODULE_NAME,
        .of_match_table = hello_spi_tftlcd_table,
    },
};

module_platform_driver(dri);

MODULE_AUTHOR("ljq");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("test spi tftlcd driver");
